package com.uenpay.im.ui.old.activity.wallet;

import com.qiyunxin.android.http.net.HttpInvokeItem;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * @function 获取收支明细
 */
public class GetPaymentsInvokItem extends HttpInvokeItem {

	public class GetPaymentsInvokItemResult extends
			HttpInvokeResult {
		public long timeStamp;
		public ArrayList<PaymentEntity> arrayList = new ArrayList<PaymentEntity>();
	}

	public GetPaymentsInvokItemResult getOutput() {
		return (GetPaymentsInvokItemResult) GetResultObject();
	}

	/**
	 * 获取收支明细
	 * @param subject
	 * @param page
	 */
	public GetPaymentsInvokItem(String subject, int page) {
		String url = APIConfiguration.getBaseUrl()
				+ "Purse/Purse/purseTurnover?subject=" + subject
				+ "&pageIndex=" + page + "&pageSize=12&"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) throws Exception {
		GetPaymentsInvokItemResult result = new GetPaymentsInvokItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.timeStamp = json.optLong("timestamp");
			JSONArray arr = json.optJSONArray("data");
			if (arr == null) {
				return result;
			}

			for (int i = 0, size = arr.length(); i < size; i++) {
				result.arrayList.add(desRedPaymentEntity(arr.optJSONObject(i)));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return result;
	}

	private PaymentEntity desRedPaymentEntity(JSONObject json) {
		PaymentEntity redPaymentEntity = new PaymentEntity();
		redPaymentEntity.amount = json.optString("amount");
		redPaymentEntity.subject = json.optString("subject");
		redPaymentEntity.id = json.optInt("id");
		redPaymentEntity.createtime = json.optString("createtime");
		redPaymentEntity.tradeno = json.optString("tradeno");
		redPaymentEntity.leftamount = json.optString("leftamount");
		return redPaymentEntity;
	}

}
